/**
 * Created by gadflybsd on 2017/3/12.
 */
var mustacheApp = angular.module('mustache.directives', []);
mustacheApp.directive('registerDom',function($rootScope, Service, unit){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			model: '=model',
			module: '=module',
			form: '=form',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/register.html';
		},
		link: function(scope, element, attrs){
			var sendButton = element.find('button.sendSms');
			sendButton.on('click', function(){
				unit.sendSmsTimer(sendButton);
				Service.sendSms({mobile: attrs.mobile, action: 'forgotPassword'}, function(resp){
					if(resp.type.toLowerCase() == 'success') unit.sendSmsTimer(sendButton);
				}, attrs.ac);
			});
		},
		controller: function($scope, $state, $window, xhr){
			$scope.register = {}
			$scope.title = $rootScope.$$childHead.$title;
			$scope.weixin = function(){
				Service.weixinLogin();
			}
			$scope.submit = function(){
				var model = (angular.isUndefined($scope.model) || $scope.model === null)?'MustachUser':$scope.model;
				var module = (angular.isUndefined($scope.module) || $scope.module === null)?'register':$scope.module;
				var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.my':$scope.back;
				var form = (angular.isUndefined($scope.form) || $scope.form === null || typeof $scope.form != 'object')?{}:$scope.form;
				var data = {model: model, module: module, data: angular.extend($scope.register, form)};
				if(!unit.isEmptyObject($window.sessionStorage.weixin)) data.data.weixin = JSON.parse($window.sessionStorage.weixin);
				xhr.service('post', data, function(resp){
					if(resp.type.toLowerCase()=='success') $state.go(back);
				})
			}
		}
	}
});
mustacheApp.directive('loginDom',function($rootScope){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			model: '=model',
			module: '=module',
			form: '=form',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/login.html';
		},
		controller: function($scope, $state, $ionicLoading, $window, xhr, Service, unit){
			$scope.login = {mobile: null, password: null};
			$scope.title = $rootScope.$$childHead.$title;
			$scope.submit = function(){
				var model = (angular.isUndefined($scope.model) || $scope.model === null)?'MustachUser':$scope.model;
				var module = (angular.isUndefined($scope.module) || $scope.module === null)?'login':$scope.module;
				var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.my':$scope.back;
				$scope.form = (angular.isUndefined($scope.form) || $scope.form === null || typeof $scope.form != 'object')?{}:$scope.form;
				var data = {model: model, module: module, data: angular.extend($scope.login, $scope.form)};
				if(!unit.isEmptyObject($window.sessionStorage.weixin)) data.data.weixin = JSON.parse($window.sessionStorage.weixin);
				xhr.service('post', data, function(resp){
					if(resp.type.toLowerCase()=='success') $state.go(back, {}, { reload: true });
				})
			};
			$scope.weixin = function(){
				Service.weixinLogin();
			}
		}
	}
});
/**
 * 实名认证指令，页面调用方法
<realname-dom model="'MustachUser'" module="'realname'" back="'app.my'" form="form" title="'实名认证标题'">
	<!-- 额外新增的表单字段开始 -->
	<label class="item-input">
		<input type="tel" name="nickname" ng-model="form.realname" placeholder="请填写真实姓名">
	</label>
	<!-- 额外新增的表单字段结束 -->
</realname-dom>
 */
mustacheApp.directive('realnameDom',function($rootScope, $ionicLoading, imgPickerService, ngSwal){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			model: '=model',
			module: '=module',
			form: '=form',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/realname.html';
		},
		controller: function($scope, $state, xhr){
			xhr.userState('register', function(user){
				$scope.title = $rootScope.$$childHead.$title;
				var model = (angular.isUndefined($scope.model) || $scope.model === null)?'MustachUser':$scope.model;
				var module = (angular.isUndefined($scope.module) || $scope.module === null)?'realnameApply':$scope.module;
				var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.my':$scope.back;
				$scope.form = (angular.isUndefined($scope.form) || $scope.form === null || typeof $scope.form != 'object')?{}:$scope.form;
				$scope.realname = {
					realname: user.realname,
					idcard: user.idcard
				};
				$scope.idcard1 = user.idcard_url_1;
				$scope.idcard2 = user.idcard_url_2;
				$scope.setIdcard1 = function(){
					imgPickerService.camera(function(res){
						$scope.idcard1 = res;
					})
				};
				$scope.setIdcard2 = function(){
					imgPickerService.camera(function(res){
						$scope.idcard2 = res;
					})
				};
				$scope.submit = function(){
					if($scope.idcard1 == 'img/no-astar.jpg'){
						ngSwal.alert({text: '身份证正面必须拍照', type: 'warning'});
					}else{
						if($scope.idcard2 == 'img/no-astar.jpg'){
							ngSwal.alert({text: '身份证背面必须拍照', type: 'warning'});
						}else{
							imgPickerService.uploads([$scope.idcard1, $scope.idcard2], '上传二代身份证图片', function(resp){
								$ionicLoading.show({ content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0 });
								$scope.realname.idcard_1 = resp[0].data.id;
								$scope.realname.idcard_2 = resp[1].data.id;
								xhr.service('post', {
									model: model,
									module: module,
									data: $scope.realname
								}, function(){
									$ionicLoading.hide();
									$state.go(back, {}, {reload: true});
								});
							});
						}
					}
				}
			}, true);
		}
	}
});
/**
 * 设定头像指令，页面调用方法
<set-headimg-dom model="'MustachUser'" module="'setHeadimg'" back="'app.my'" form="form" title="'设定头像标题'">
	<!-- 额外新增的表单字段开始 -->
	<label class="item-input">
		<input type="tel" name="nickname" ng-model="form.realname" placeholder="请填写真实姓名">
	</label>
	<!-- 额外新增的表单字段结束 -->
</set-headimg-dom>
 */
mustacheApp.directive('setHeadimgDom',function($rootScope){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			model: '=model',
			module: '=module',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/headimg.html';
		},
		controller: function($scope, $state, imgPickerService, xhr){
			xhr.userState('login', function(user){
				$scope.title = $rootScope.$$childHead.$title;
				$scope.headimg = user.headimg;
				var model = (angular.isUndefined($scope.model) || $scope.model === null)?'MustachUser':$scope.model;
				var module = (angular.isUndefined($scope.module) || $scope.module === null)?'setHeadimg':$scope.module;
				var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.my':$scope.back;
				$scope.setHeadimg = function () {
					imgPickerService.headimg(function(url){
						imgPickerService.uploads([url], '上传个人头像图片', function(resp){
							xhr.service('post', {model: model, module: module, data: {headimg: resp[0].data.id}}, function(res){
								if(res.type.toLowerCase()=='success')
									if(back != 'notJust') $state.go(back, {}, { reload: true });
							});
						});
					})
				}
			}, true);
		}
	}
});

mustacheApp.directive('setPasswordDom',function($rootScope, Service, unit){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			model: '=model',
			module: '=module',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/password.html';
		},
		link: function(scope, element, attrs){
			var sendButton = element.find('button.sendSms');
			sendButton.on('click', function(){
				unit.sendSmsTimer(sendButton);
				Service.sendSms({mobile: attrs.mobile, action: 'changePassword'}, function(resp){
					if(resp.type.toLowerCase() == 'success') unit.sendSmsTimer(sendButton);
				}, attrs.ac);
			});
		},
		controller: function($scope, $state, xhr){
			xhr.userState('login', function(user){
				$scope.form = {mobile: user.mobile};
				$scope.title = $rootScope.$$childHead.$title;
				var model = (angular.isUndefined($scope.model) || $scope.model === null)?'MustachUser':$scope.model;
				var module = (angular.isUndefined($scope.module) || $scope.module === null)?'setPassword':$scope.module;
				var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.my':$scope.back;
				$scope.submit = function(){
					xhr.service('put', {model: model, module: module, data: $scope.form}, function(res){
						if(res.type.toLowerCase()=='success') $state.go(back, {}, { reload: true });
					});
				}
			}, true);
		}
	}
});

mustacheApp.directive('forgotPasswordDom',function($rootScope, Service, unit){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			model: '=model',
			module: '=module',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/forgot-password.html';
		},
		link: function(scope, element, attrs){
			var sendButton = element.find('button.sendSms');
			sendButton.on('click', function(){
				unit.sendSmsTimer(sendButton);
				Service.sendSms({mobile: attrs.mobile, action: 'forgotPassword'}, function(resp){
					if(resp.type.toLowerCase() == 'success') unit.sendSmsTimer(sendButton);
				}, attrs.ac);
			});
		},
		controller: function($scope, $state, xhr){
			$scope.form = {};
			$scope.title = $rootScope.$$childHead.$title;
			var model = (angular.isUndefined($scope.model) || $scope.model === null)?'MustachUser':$scope.model;
			var module = (angular.isUndefined($scope.module) || $scope.module === null)?'forgotPassword':$scope.module;
			var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.my':$scope.back;
			$scope.submit = function(){
				xhr.service('put', {model: model, module: module, data: $scope.form}, function(res){
					if(res.type.toLowerCase()=='success') $state.go(back, {}, { reload: true });
				});
			}
		}
	}
});

mustacheApp.directive('setMobileDom',function($rootScope, sendSms){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			model: '=model',
			module: '=module',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/mobile.html';
		},
		link: function(scope, element, attrs){
			var sendButton = element.find('button.sendSms');
			sendButton.on('click', function(){
				sendSmsTimer(sendButton);
				/*sendSms.ctc(attrs.mobile, function(resp){
				 if(resp.type.toLowerCase() == 'success') sendSmsTimer();
				 }, attrs.ac);*/
			})
		},
		controller: function($scope, $state, xhr){
			xhr.userState('login', function(user){
				$scope.model = {mobile: user.mobile};
				$scope.title = $rootScope.$$childHead.$title;
				var model = (angular.isUndefined($scope.model) || $scope.model === null)?'MustachUser':$scope.model;
				var module = (angular.isUndefined($scope.module) || $scope.module === null)?'setMobile':$scope.module;
				var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.my':$scope.back;
				$scope.submit = function(){
					xhr.service('put', {model: model, module: module, data: $scope.model}, function(res){
						if(res.type.toLowerCase()=='success') $state.go(back, {}, { reload: true });
					});
				}
			}, true);
		}
	}
});

mustacheApp.directive('setNicknameDom',function($rootScope){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			model: '=model',
			module: '=module',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/nickname.html';
		},
		controller: function($scope, $state, xhr){
			xhr.userState('login', function(user){
				$scope.nickname = user.nickname;
				$scope.title = $rootScope.$$childHead.$title;
				var model = (angular.isUndefined($scope.model) || $scope.model === null)?'MustachUser':$scope.model;
				var module = (angular.isUndefined($scope.module) || $scope.module === null)?'setNickname':$scope.module;
				var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.my':$scope.back;
				$scope.submit = function(){
					xhr.service('put', {model: model, module: module, data: {nickname: $scope.nickname}}, function(res){
						if(res.type.toLowerCase()=='success') $state.go(back, {}, { reload: true });
					});
				}
			}, true);
		}
	}
});

/**
 * 设定头像指令，页面调用方法
 <set-headimg-dom model="'MustachUser'" module="'setHeadimg'" back="'app.my'" form="form" title="'设定头像标题'">
 <!-- 额外新增的表单字段开始 -->
 <label class="item-input">
 <input type="tel" name="nickname" ng-model="form.realname" placeholder="请填写真实姓名">
 </label>
 <!-- 额外新增的表单字段结束 -->
 </set-headimg-dom>
 */
mustacheApp.directive('myProfileDom',function(){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			user: '=user',
			state: '=state',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/myProfile.html';
		},
		controller: function($scope, $rootScope, Service){
			$scope.title = $rootScope.$$childHead.$title;
			$scope.weixin = function(){
				Service.bindWeixin();
			};
			var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.home':$scope.back;
			$scope.logout = function(){
				Service.logout(back, function(user, identity){
					$scope.state = identity;
					$scope.user = user;
				});
			}
		}
	}
});

mustacheApp.directive('myCenterDom',function($rootScope){
	return {
		restrict: 'AEC',
		scope: {
			back: '=back',
			user: '=user',
			state: '=state',
			title: '=title'
		},
		transclude: true,
		templateUrl: function(){
			return 'javascript/directives/user-tpl/myCenter.html';
		},
		controller: function($scope, $state, $window, xhr, Service){
			xhr.userState('login', function(user){
				$scope.title = $rootScope.$$childHead.$title;
				$scope.weixin = function(){
					Service.bindWeixin();
				};
				var back = (angular.isUndefined($scope.back) || $scope.back === null)?'app.home':$scope.back;
				$scope.logout = function(){
					Service.logout(back, function(user, identity){
						$scope.state = identity;
						$scope.user = user;
					});
				}
			}, true);
		}
	}
});